package com.company.vmvp.base;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.company.vmvp.R;
import com.company.vmvp.ui.main.entity.UserBean;
import com.company.vmvp.tools.AppTools;
import com.company.vmvp.tools.PermissionTools;
import com.dee.components.base.mvp.BasePresenter;
import com.dee.components.base.ui.ActivityBase;
import com.dee.components.util.PermissionsUtils;
import com.dee.components.widget.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：kkan on 2017/01/30
 * 当前类注释:
 * 基类Activity
 */
public abstract class BaseActivity<T extends BasePresenter> extends ActivityBase<T> {

    @Nullable
    @BindView(R.id.toolbar_title_view)
    TitleView toolbarTitleView;
    @Nullable
    @BindView(R.id.toolbar_title_ll)
    LinearLayout toolbarTitleLl;
    @Nullable
    @BindView(R.id.toolbar_line)
    TextView toolbarLine;

    public UserBean userBean;
    public PermissionTools permissionTools;

    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    public void doBeforeContentView() {
        super.doBeforeContentView();
        permissionTools = new PermissionTools(this);
        // 设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    public void doAfterContentView() {
        super.doAfterContentView();
        ButterKnife.bind(this);
        upToolBarLeftFinish();

        // android 8.0 windowIsTranslucent 与 设置竖屏 冲突
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
            swipeDragToClose();
        }

        userBean = AppTools.getUserBean();
        immersive(toolbarTitleView, false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        userBean = AppTools.getUserBean();
    }

    public void upToolBarLeftFinish(){
        if (toolbarTitleView != null) {
            toolbarTitleView.setLeftIconVisibility(true);
            toolbarTitleView.setLeftTextVisibility(true);
            //设置返回点击事件
            toolbarTitleView.setLeftFinish(context);
        }
    }

    public String getIntentString(String name){
        return getIntent().getStringExtra(name);
    }

    public void showTipDialog(CharSequence message){
        showTipDialog("系统提示",message,"确定","",true,null,null);
    }

    public void showTipDialog(CharSequence message, View.OnClickListener leftlistener){
        showTipDialog("系统提示",message,"确定","取消",true,leftlistener,null);
    }
    public void showTipDialog(String title, CharSequence message, View.OnClickListener leftlistener){
        showTipDialog(title,message,"确定","取消",true,leftlistener,null);
    }

    public void showTipDialog(String title, CharSequence message,String leftStr, View.OnClickListener leftlistener){
        showTipDialog(title,message,leftStr,"取消",true,leftlistener,null);
    }


    public void showTipDialog(String title,CharSequence message,String leftStr,String rightStr,boolean cancelableOutside,View.OnClickListener leftlistener,View.OnClickListener rightlistener){

        creatDialogBuilder()
                .setDialog_title(title)
                .setDialog_message(message)
                .setDialog_Left(leftStr)
                .setLeftlistener(leftlistener)
                .setDialog_Right(rightStr)
                .setRightlistener(rightlistener)
                .setCancelableOutside(cancelableOutside)
                .show();

    }

    /*
     * 注册权限申请回调
     *
     * @param requestCode  申请码
     * @param permissions  申请的权限
     * @param grantResults 结果
     * */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PermissionsUtils.PermissionsRequestCode) {

            for (int i = 0; i < permissions.length; i++) {
                String permission = permissions[i];
                //grantResults[i] == PackageManager.PERMISSION_DENIED 拒绝权限
                //grantResults[i] == PackageManager.PERMISSION_GRANTED 通过权限
                boolean granted=grantResults[i] == PackageManager.PERMISSION_GRANTED;
                if (permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    //通过文件读写权限

                }else if (permission.equals(Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS)) {

                }
            }
        }
    }
}
